package com.yanqu.road.dao.impl.activity.liondance;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.liondance.CrossLionDanceAllData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class CrossLionDanceAllDataDaoImpl extends TempDataObjectDao<CrossLionDanceAllData> {

    public static String getCrossLionDanceAllDataSql = "select * from `t_c_activity_lion_dance_all_data` where `activityId`=?";
    public CrossLionDanceAllData getCrossLionDanceAllData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossLionDanceAllDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    public static String updateCrossLionDanceAllDataSql = "update `t_c_activity_lion_dance_all_data` set allMakeNum=?,allProcess=?,updateTime=? where activityId=?";
    @Override
    protected boolean updateData(CrossLionDanceAllData crossLionDanceAllData) {
        crossLionDanceAllData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceAllData.getAllMakeNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceAllData.getAllProcess()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceAllData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceAllData.getActivityId()));
        if (getDb().executeNoneQuery(updateCrossLionDanceAllDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossLionDanceAllDataSql = "insert into `t_c_activity_lion_dance_all_data`(activityId,allMakeNum,allProcess,updateTime) values (?,?,?,?)";
    @Override
    protected boolean addData(CrossLionDanceAllData crossLionDanceAllData) {
        crossLionDanceAllData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceAllData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceAllData.getAllMakeNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceAllData.getAllProcess()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceAllData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertCrossLionDanceAllDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossLionDanceAllData parseData(ResultSet rs) throws SQLException {
        CrossLionDanceAllData data = new CrossLionDanceAllData();
        data.setActivityId(rs.getInt("activityId"));
        data.setAllMakeNum(rs.getLong("allMakeNum"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setAllProcess(rs.getLong("allProcess"));
        return data;
    }
}
